import sys
import time

from DrissionPage import ChromiumPage
import pymysql
import pyautogui
from bs4 import BeautifulSoup
import re
import logging
from logging.handlers import TimedRotatingFileHandler
from tqdm import tqdm

log_file = 'douyin_video_comment.log'

# 创建一个处理日志的处理器
log_handler = TimedRotatingFileHandler(log_file, when='midnight', interval=1, backupCount=7)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_formatter)
log_handler.setLevel(logging.INFO)

# 获取日志记录器并添加处理器
logger = logging.getLogger()
logger.addHandler(log_handler)
logger.setLevel(logging.INFO)
# 设置超时时间为10分钟
timeout = 5 * 60
# 获取视频ID，转换为整数

def filter_comment(comment):
    """
    过滤评论中的非法字符
    """
    # 使用正则表达式替换非法字符为空字符串
    filtered_comment = re.sub(r'[^\u4e00-\u9fa5a-zA-Z0-9.,!?;:()\'"\-_ ]', '', comment)
    return filtered_comment


mydb = pymysql.connect(
    host="rm-2zea30h4sh8g15zd1ho.mysql.rds.aliyuncs.com",  # MySQL host
    port=3306,  # MySQL port
    user="root",  # MySQL username
    password="Ds2024@()833429",  # MySQL password
    database="douyinpinglun",
)
cursor = mydb.cursor()
driver = ChromiumPage()
driver.set.window.max()
print('数据库连接成功')
id_list = [28,
           63,
           69,
           71,
           76,
           96,
           118,
           119,
           122,
           155,
           200,
           204,
           209,
           226,
           231,
           242,
           246,
           250,
           263,
           268,
           278,
           296,
           545,
           603,
           1754,
           1815,
           1894,
           1947,
           1966,
           1976,
           1994,
           2058,
           2533,
           2549,
           2590,
           2608,
           2636,
           2660,
           4115,
           4118,
           4167,
           4185,
           4194,
           4223,
           4248,
           4249,
           4304,
           4323,
           4346,
           4352,
           4354,
           4391,
           4406,
           4408,
           4420,
           4467,
           4502,
           4508,
           4570,
           4648,
           4651,
           4670,
           4706,
           4711,
           4757,
           4764,
           4793,
           4811,
           4820,
           4877,
           4932,
           5053,
           5192,
           5222,
           5253,
           5289,
           5356,
           5361,
           5385,
           5466,
           5470,
           5473,
           5569,
           5616,
           5643,
           6119,
           6159,
           6275,
           6289,
           6367,
           6429,
           6433,
           6446,
           6458,
           6502,
           6582,
           6606,
           6636,
           6758,
           6772,
           6795,
           6854,
           6872,
           6875,
           6950,
           6959,
           7005,
           7049,
           7121,
           7158,
           7167,
           7170,
           7174,
           7183,
           7263,
           7265,
           7335,
           7359,
           7420,
           7464,
           7489,
           7552,
           7595,
           7602,
           7608,
           7722,
           7725,
           7745,
           7778,
           7780,
           7805,
           7812,
           7817,
           7830,
           7921,
           8014,
           8033,
           8034,
           8068,
           8103,
           8119,
           8121,
           8147,
           8149,
           8158,
           8194,
           8214,
           8238,
           8252,
           8321,
           8384,
           8413,
           8417,
           8547,
           9207,
           9235,
           9247,
           9277,
           9344,
           9511,
           9542,
           9656,
           9694,
           9841,
           10080,
           10164,
           10181,
           10193,
           10269,
           10328,
           10344,
           10388,
           10436,
           10485,
           10491,
           10507,
           10525,
           10544,
           10569,
           10593,
           10596,
           10614,
           10630]
for id in id_list:
    cursor.execute("select id,视频链接,评论量 from 抖音视频表 where id=%s", (id,))
    videoid_url = cursor.fetchone()
    # 关联视频表id
    videoid = videoid_url[0]
    print(f'当前正在爬取id为{videoid}的视频')
    logger.info(f'当前正在爬取id为{videoid}的视频')

    # 视频链接
    video_url = videoid_url[1]
    # 评论数量
    comment_number = int(videoid_url[2])
    driver.get(video_url)
    driver.wait(7)
    try:
        if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_video.png', confidence=0.7):
            print('该视频已下架')
            insert_sql = '''
                UPDATE 抖音视频表
                SET 是否下架=%s
                WHERE ID = %s;
                '''
            cursor.execute(insert_sql, ('已下架', videoid))
            mydb.commit()
            continue
    except:
        pass
    # 评论
    try:
        comment = driver.ele(
            'xpath://*[@id="douyin-right-container"]/div[2]/div/div/div[1]/div[3]/div/div[2]/div[1]/div[2]/span').text
    except:
        continue
    if '首评' in comment:
        continue
    try:
        if '万' in comment:
            pnumber2 = int(float(comment.split('万')[0]) * 10000)
        else:
            pnumber2 = int(comment)
    except:
        continue
    # 点赞
    dianzhan = driver.ele(
        'xpath://*[@id="douyin-right-container"]/div[2]/div/div/div[1]/div[3]/div/div[2]/div[1]/div[1]/span').text
    try:
        if '万' in dianzhan:
            dnumber1 = int(float(dianzhan.split('万')[0]) * 10000)
        else:
            dnumber1 = int(dianzhan)
    except:
        pass

    # 收藏
    collect = driver.ele(
        'xpath://*[@id="douyin-right-container"]/div[2]/div/div/div[1]/div[3]/div/div[2]/div[1]/div[3]/span').text
    try:
        if '万' in collect:
            snumber3 = int(float(collect.split('万')[0]) * 10000)
        else:
            snumber3 = int(collect)
    except:
        pass

    # 转发
    share = driver.ele(
        'xpath://*[@id="douyin-right-container"]/div[2]/div/div/div[1]/div[3]/div/div[2]/div[1]/div[4]/span').text
    try:
        if '万' in share:
            znumber4 = int(float(share.split('万')[0]) * 10000)
        else:
            znumber4 = int(share)
    except:
        pass
    if pnumber2 == 0:
        continue
    video_date_text = driver.ele(
        'xpath://*[@id="douyin-right-container"]/div[2]/div/div/div[1]/div[3]/div/div[2]/div[2]/span').text
    video_date = video_date_text.replace('发布时间：', '')
    print(video_date)
    insert_sql = '''
    UPDATE 抖音视频表
    SET 视频发布时间_2=%s
    WHERE ID = %s;
    '''
    cursor.execute(insert_sql, (video_date, videoid))
    mydb.commit()

    pyautogui.moveTo(1688, 904)
    start_time = time.time()
    while True:
        if time.time() - start_time > timeout:
            break
        try:
            more_location = pyautogui.locateOnScreen(r'pictures/reply.png', confidence=0.8)
            pyautogui.click(more_location)
            driver.wait(0.8)
        except:
            try:
                more_comment_location = pyautogui.locateOnScreen(r'pictures/more.png', confidence=0.77)
                pyautogui.click(more_comment_location)
                driver.wait(0.4)
            except:
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_more_comment.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_more_comment2.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_comment3.png',
                                                confidence=0.8):
                        break
                except:
                    pass
                try:
                    if pyautogui.locateOnScreen(r'E:\pythonproject\douyingpinglun\pictures\no_comment4.png',
                                                confidence=0.8):
                        break
                except:
                    pass
        pyautogui.scroll(-300)
        driver.wait(0.8)

    comment_bolcks = driver.ele('.Rwb9ssMc comment-mainContent').eles('.UuCzPLbi')
    # 各个评论对,也就是一块块评论块
    for comment_bolck_index, comment_block in enumerate(comment_bolcks):
        html_content_text = comment_block.html
        soup = BeautifulSoup(html_content_text, 'lxml')
        comments = soup.findAll('div', class_='Vrj4Q3zT fiDvPS80')
        # 评论块里的每一行
        comment_history = set()
        for comment_item in comments:
            authors = comment_item.findAll('a', class_='uz1VJwFY')
            # 用户昵称
            author_name = authors[0].get_text(strip=True)
            print(f'作者:{author_name}')
            author_name = filter_comment(author_name)
            # 评论内容
            try:
                comment_text = comment_item.find('span', class_='WFJiGxr7').get_text(strip=True)
            except:
                comment_text = None
                continue
            print(f'评论:{comment_text}')
            # 过滤评论内容
            comment_text = filter_comment(comment_text)
            if not comment_text or comment_text in comment_history:
                continue
            # 评论时间和地点
            comment_history.add(comment_text)
            comment_time_space = comment_item.find('div', class_='fJhvAqos').get_text(strip=True)
            comment_time = comment_time_space.split('·')[0]
            try:
                comment_space = comment_time_space.split('·')[1]
            except:
                comment_space = None
            print(f'时间:{comment_time}')
            print(f'地点:{comment_space}')
            # 用户点赞数
            comment_support_number = comment_item.find('p', class_='xZhLomAs').get_text(strip=True)
            if '万' in comment_support_number:
                comment_support_number = int(float(comment_support_number.split('万')[0]) * 10000)
            else:
                comment_support_number = int(comment_support_number)

            print(f'点赞数:{comment_support_number}')
            print(f'问答对编号:{comment_bolck_index}')
            # 插入 SQL 语句
            insert_sql = '''
                INSERT INTO 视频评论表 
                (关联视频表id, 评论内容, 用户昵称, 评论点赞数, 评论时间, 评论地点, 问答对编号)
                VALUES (%s, %s, %s, %s, %s, %s, %s);
                '''
            # 执行插入操作
            try:
                cursor.execute(insert_sql, (
                    videoid, comment_text, author_name, comment_support_number, comment_time, comment_space,
                    comment_bolck_index))
                mydb.commit()  # 提交事务
            except:
                pass

cursor.close()
mydb.close()
#
# driver.get('https://live.douyin.com/889398542120')
# driver.set.window.max()
